﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using ERP_RORZE_DLL.Product.DAO;
using ERP_RORZE_PERMISSION.Base;
using ERP_RORZE_PRODUCT.Product.database;
using System.Data.OleDb;
using ERP_RORZE_SYSTEM;
using System.Data.Common;
using System.Data.SqlClient;
using ERP_RORZE_DLL.Product.Base;

namespace ERP_RORZE_PRODUCT.Product.BasicList
{
    public partial class ctrlCustomer : DevExpress.XtraEditors.XtraUserControl
    {
        ClsBasicListDAO clsBasiclistDAO = new ClsBasicListDAO();
        ClsCustomerTable b = new ClsCustomerTable();
        private LogUtils log;
        DataTable dt;

        public ctrlCustomer()
        {
            InitializeComponent();
            log = new LogUtils();
        }

        private void btDelete_Click(object sender, EventArgs e)
        {
            b.CustomerID = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "CustomerID").ToString();
            clsBasiclistDAO.DeleteCustomer(b);

            //write Log
            log.Table = LogConstant.Table.Product.Customer;
            log.Task = LogConstant.Task.Delete;
            log.Description = b.CustomerID;
            log.WriteLog();

            Reload(sender, e);
        }

        private void btnRefresh_Click(object sender, EventArgs e)
        {
            Reload(sender, e);
        }

        private void btnImport_Click(object sender, EventArgs e)
        {
            FileDialog fileDialog = new OpenFileDialog();
            fileDialog.Filter = "Excel 2007-2013|*.xlsx|Excel 97-2003 (*.xls)|*.xls";
            if (fileDialog.ShowDialog() == DialogResult.OK)
            {
                OleDbConnection connection = new OleDbConnection();
                try
                {
                    string path = fileDialog.FileName;
                    INISettings iniSettings = new INISettings();
                    string connectString = iniSettings.SQLConnectionString;
                    // Connection String to Excel Workbook
                    string excelConnectionString = string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=Excel 12.0;Persist Security Info=False", path);

                    connection.ConnectionString = excelConnectionString;
                    OleDbCommand command = new OleDbCommand("select * from [Sheet1$]", connection);
                    OleDbCommand commandCount = new OleDbCommand("select  COUNT(*) from [Sheet1$]", connection);

                    connection.Open();
                    int rowCount = (int)commandCount.ExecuteScalar();
                    // Create DbDataReader to Data Worksheet
                    DbDataReader dr = command.ExecuteReader();

                    // Bulk Copy to SQL Server 
                    SqlBulkCopy bulkInsert = new SqlBulkCopy(connectString);
                    bulkInsert.DestinationTableName = "[rrc_database].[dbo].[242_Customer]";

                    SqlBulkCopyColumnMapping date = new SqlBulkCopyColumnMapping("CustomerID", "CustomerID");
                    bulkInsert.ColumnMappings.Add(date);

                    SqlBulkCopyColumnMapping toolId = new SqlBulkCopyColumnMapping("CustomerName", "CustomerName");
                    bulkInsert.ColumnMappings.Add(toolId);

                    SqlBulkCopyColumnMapping makerId = new SqlBulkCopyColumnMapping("CusInfo", "CusInfo");
                    bulkInsert.ColumnMappings.Add(makerId);

                    bulkInsert.WriteToServer(dr);

                    connection.Close();

                    //write Log
                    log.Table = LogConstant.Table.Product.Customer;
                    log.Task = LogConstant.Task.Import;
                    log.Description = string.Format(LogConstant.Message.Import, rowCount.ToString(), "");
                    log.WriteLog();

                    Reload(sender, e);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                    connection.Close();
                }
            }
        }

        private void btnAddNew_Click(object sender, EventArgs e)
        {
            frm_CustomerAdd frmAdd = new frm_CustomerAdd();
            frmAdd.Reload += new EventHandler(Reload);
            frmAdd.ShowDialog();
        }

        private void panelControl1_Paint(object sender, PaintEventArgs e)
        {

        }

        private void gridControl1_Click(object sender, EventArgs e)
        {

        }

        private void btEdit_Click(object sender, EventArgs e)
        {
            frm_CustomerEdit frmEdit = new frm_CustomerEdit();
            frmEdit.ID = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "CustomerID").ToString();
            frmEdit.Name = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "CustomerName").ToString();
            frmEdit.Info = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "CusInfo").ToString();
            frmEdit.Reload += new EventHandler(Reload);
            frmEdit.ShowDialog();
        }

        private void ctrlCustomer_Load(object sender, EventArgs e)
        {
            Reload(sender, e);
        }
        void Reload(object sender, EventArgs e)
        {
            dt = clsBasiclistDAO.GetAllCustomer();
            gridControl1.DataSource = dt;
            gridView1.BestFitColumns();
        }

        private void btnReload_Click(object sender, EventArgs e)
        {
            Reload(sender, e);
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            btnAddNew_Click(sender, e);
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            btDelete_Click(sender, e);
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            btEdit_Click(sender, e);
        }

        private void btnExport_Click(object sender, EventArgs e)
        {
            clsConstantsFuction.ExportToExcel(gridView1, 1, true);
        }
    }
}
